using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HitGUI : MonoBehaviour {
	
	public bool debug = false;
	
	public Texture2D bloodTextureUp;
	public Texture2D bloodTextureLeft;
	public Texture2D bloodTextureRight;
	public Texture2D bloodTextureDown;
	
	//Timers for damage effects
	private float timerUp 		= 0;
	private float timerLeft 	= 0;
	private float timerRight 	= 0;
	private float timerDown 	= 0;
	
	// How long will the textures show
	public float bloodTimer		= 1;
	
	void Start () {
		
		// Check if textures are empty
		checkTexture(ref bloodTextureUp);
		checkTexture(ref bloodTextureLeft);
		checkTexture(ref bloodTextureRight);
		checkTexture(ref bloodTextureDown);
	
	}
	
	//If the texture still has an active timer - display the texture
	void OnGUI(){
		// How much space should be removed for the upper and lower textures
		float screenCropper 	= Screen.width*0.25f;
		// How much the height should be scaled for the upper and lower textures
		float upDownscaler 		= (float)(Screen.width-screenCropper)/(float)bloodTextureUp.width;
		// How much the width should be scaled for the left and right textures
		float leftRightscaler 	= (float)Screen.height/(float)bloodTextureLeft.height;
		
		// If timer is still active: draw textures.
		if (timerLeft > Time.time){
			GUI.color = new Color(1,1,1,(timerLeft-Time.time)/bloodTimer);
			GUI.DrawTexture(new Rect(0,0,bloodTextureLeft.width*leftRightscaler,Screen.height),bloodTextureLeft,ScaleMode.StretchToFill);
			GUI.color = Color.white;
		}
		if (timerRight > Time.time){
			GUI.color = new Color(1,1,1,(timerRight-Time.time)/bloodTimer);
			GUI.DrawTexture(new Rect(Screen.width-bloodTextureRight.width*leftRightscaler,0,bloodTextureRight.width*leftRightscaler,Screen.height),bloodTextureRight,ScaleMode.StretchToFill);
			GUI.color = Color.white;	
		}
		if (timerUp > Time.time){
			GUI.color = new Color(1,1,1,(timerUp-Time.time)/bloodTimer);
			GUI.DrawTexture(new Rect(screenCropper/2,0,Screen.width-screenCropper,bloodTextureUp.height*upDownscaler),bloodTextureUp,ScaleMode.StretchToFill);
			GUI.color = Color.white;
		}
		if (timerDown > Time.time){
			GUI.color = new Color(1,1,1,(timerDown-Time.time)/bloodTimer);
			GUI.DrawTexture(new Rect(screenCropper/2,Screen.height-bloodTextureDown.height*upDownscaler,Screen.width-screenCropper,bloodTextureDown.height*upDownscaler),bloodTextureDown,ScaleMode.StretchToFill);		
			GUI.color = Color.white;
		}
		// TestUI
		if (debug) {
			GUI.BeginGroup(new Rect(Screen.width/2,Screen.height-250,125,125));
				if (GUI.Button(new Rect(0,50,25,25),"")){
					timerLeft = Time.time + bloodTimer;
				}
				if (GUI.Button(new Rect(100,50,25,25),"")){
					timerRight = Time.time + bloodTimer;
				}
				if (GUI.Button(new Rect(50,0,25,25),"")){
					timerUp = Time.time + bloodTimer;
				}
				if (GUI.Button(new Rect(50,100,25,25),"")){	
					timerDown = Time.time + bloodTimer;
				}
			GUI.EndGroup();
		}	
	}
	
	void checkTexture(ref Texture2D texture){
		// Check if textures are empty and replace them if that's the case.
		if (texture == null){
			texture = new Texture2D(1,1);
			texture.SetPixel(0,0,Color.red);
			texture.Apply();
		}
	}
	
	public void displayHit(Vector3 hitVector3, Vector3 cameraPosition, Vector3 cameraDirection){
		//Calculate angle and set timers for textures.
		Vector2 hit = new Vector2(hitVector3.x,hitVector3.z);
		Vector2 dir = new Vector2(cameraDirection.x,cameraDirection.z);
		float angle = Vector2.Angle(dir,-hit);
		Vector3 cross = Vector3.Cross(dir,-hit);
		if (angle >= 135)
			timerDown = Time.time + bloodTimer;
		if (angle <= 45)
			timerUp = Time.time + bloodTimer;
		if (cross.z > 0)
			angle = 360 - angle;
		if (angle > 45 && angle < 135)
			timerRight = Time.time + bloodTimer;
		if (angle > 225 && angle < 315)
			timerLeft = Time.time + bloodTimer;
	}
	
	
}
